package com.neuronrobotics.opengl.scene;

import android.opengl.GLES20;
import android.util.Log;

public class CircleShape extends ArrayShapeNode {
	
	static int num_segments = 1000;

	public static CircleShape getCircleShape(GLHandles theHandles, float centX, float centY, float centZ, float rad){
		
		float arc_angle = (float)Math.PI*2;
		float start_angle = 0;
		
		float[] coords = new float[num_segments*3*2];
		float[] norms = new float[num_segments*3*2];
		
		float theta = arc_angle / (num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open

		float tangetial_factor = (float)Math.tan(theta);

		float radial_factor = (float)Math.cos(theta);
		
		float x = rad * (float)Math.cos(start_angle);//we now start at the start angle
		float z = rad * (float)Math.sin(start_angle); 
	    
		for(int ii = 0; ii < num_segments*2; ii++)
		{ 
			//glVertex2f(x + cx, y + cy);
			coords[ii*3] = centX + x;
			coords[ii*3+1] = centY;
			coords[ii*3+2] = centZ + z;
			
			norms[ii*3] = 0;
			norms[ii*3+1] = 1;
			norms[ii*3+2] = 0;
			
			float tx = -z; 
			float tz = x; 

			x += tx * tangetial_factor; 
			z += tz * tangetial_factor; 

			x *= radial_factor; 
			z *= radial_factor; 
		} 
				
		CircleShape newShape = new CircleShape(theHandles, coords, norms, GLES20.GL_LINE_STRIP);
		newShape.radius = rad;
		return newShape;
		
	}

	private float radius;
	
	private CircleShape(GLHandles handles, float[] coords, float[] norms,
			int drawType) {
		super(handles, coords, norms, drawType);
	}
	
	public float getRadius(){
		return radius;
	}
	
	public void setStartEndAngle(float startRad, float endRad){
		//TODO
		int startIndex = (int)((startRad/(Math.PI*2))*num_segments)%num_segments;
		int endIndex = (int)((endRad/(Math.PI*2))*num_segments)%num_segments;
		
		if(endIndex<startIndex) endIndex += num_segments;
		
		Log.d("CIRC","Start index calculated: "+startIndex);
		Log.d("CIRC","End index calculated: "+endIndex);
		
		this.drawStart = startIndex;
		this.drawEnd = endIndex - startIndex;
	}
	

	public int getNumSegments() {
		return this.num_segments;
	}
		
		
	
}
